package com.supply.dl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.supply.dl.R;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.BaseItemBean;
import com.supply.dl.bean.CheckManagerBean;
import com.supply.dl.bean.request.CheckManagerRequest;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.CommonNormalAdapterUtil;
import com.supply.dl.utils.RecyclerViewUtil;
import com.supply.dl.utils.utils.ToastUtil;
import com.supply.dl.widget.LoadTipFailedLayout;
import com.supply.dl.widget.adapter.CommonNormalAdapter;

import java.util.ArrayList;
import java.util.List;

public class CheckManagerActivity extends BaseActivity implements View.OnClickListener
{
	/**
	 * 搜索内容输入框
	 */
	@BindView(R.id.common_search_main_searchContentEdt) EditText editSearch;

	@BindView(R.id.layout_load_failed) LoadTipFailedLayout loadTipFailedLayout;
	/**
	 * 搜索按钮
	 */
	@BindView(R.id.common_search_main_searchTv) TextView txtSearch;

	/**
	 * 列表显示
	 */
	@BindView(R.id.swipe_target) RecyclerView recyclerView;

	@BindView(R.id.swipeToLoadLayout)
	SwipeToLoadLayout swipeToLoadLayout;

	private int currentPageNum = 1;
	private boolean hasMoreData = true;

	CommonNormalAdapter commonNormalAdapter;
	List<BaseItemBean> itemList = new ArrayList<BaseItemBean>();
	/**
	 * 将要搜索的内容
	 */
	private String searchContent;

	@Override protected int getLayoutResource()
	{
		return R.layout.activity_change_depot_take_delivery;
	}

	@Override protected void onCreate(@Nullable Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setTitleShow(getResourcesString(R.string.manager_check));
		showLeftBack();
		recyclerView.setLayoutManager(new LinearLayoutManager(this));
		txtSearch.setOnClickListener(this);

		swipeToLoadLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				com.supply.dl.utils.utils.T.d("onLoadMore==>");
//				if(searchModle.hasMoreData()){
//					searchModle.increasePage();
//					searchModle.vistData(keyWord, filerMaps, null);
//				}else {
//					ToastUtil.showToast(SearchActivity.this, "没有更多数据了");
//					swipeToLoadLayout.setLoadingMore(false);
//				}
				if(hasMoreData){
					currentPageNum++;
					trySearch();
				}else{
					ToastUtil.showToast(CheckManagerActivity.this, "没有更多数据了");
					swipeToLoadLayout.setLoadingMore(false);
				}


			}
		});

		showRightText("新建盘点", this);
		loadTipFailedLayout.setRestartOnClickListener(this);
		trySearch();
	}

	@Override public void onClick(View v)
	{
		super.onClick(v);
		if (v == txtSearch)
		{
			trySearch();
		} else if (v == right_tv) {
			startActivity(new Intent(this, BuildCheckActivity.class));
		}
	}

	@Override protected void initRecyclerView()
	{
		super.initRecyclerView();
		RecyclerViewUtil.initFullLinearLayoutVERTICAL(this, recyclerView);

		commonNormalAdapter = CommonNormalAdapterUtil.initCommonNormalAdapter(R.layout.item_check_manager, itemList, 43);
		View notDataView = getLayoutInflater().inflate(R.layout.empty_view_main_textview, (ViewGroup) recyclerView.getParent(), false);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
		notDataView.setLayoutParams(layoutParams);
		recyclerView.setAdapter(commonNormalAdapter);

		commonNormalAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener()
		{
			@Override public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i)
			{
				switch (view.getId())
				{
				case R.id.layout_check_root:

					Intent intent = new Intent(CheckManagerActivity.this, CheckDetailsActivity.class);
					intent.putExtra(CheckDetailsActivity.CHECK_ID, ((CheckManagerBean) itemList.get(i).object).id);
					startActivity(intent);
					break;
				default:
					break;
				}
			}
		});
	}

	/**
	 * 尝试搜索
	 */
	private void trySearch()
	{
		loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_PROGRESS);
		searchContent = editSearch.getText().toString().trim();
		HttpManger httpManger = HttpManger.getInstance();
		httpManger.setContext(this);
		CheckManagerRequest checkManagerRequest = new CheckManagerRequest();
		httpManger.getCheckMangerList(checkManagerRequest, String.valueOf(currentPageNum));
		httpManger.setHttpResponseCallBackListener(new HttpManger.HttpResponseCallBackListener()
		{
			@Override public void onSuccess(BaseInfo<?> baseInfo)
			{
				List<CheckManagerBean> transGoodsBeanList = (List<CheckManagerBean>) baseInfo.getData();
				if(currentPageNum == 1){
					itemList.clear();
				}
				if(transGoodsBeanList.size() < 10){
					hasMoreData = false;
				}
				swipeToLoadLayout.setLoadingMore(false);

				if (null != transGoodsBeanList && transGoodsBeanList.size() > 0)
				{
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FINISH);
					for (CheckManagerBean locationBean : transGoodsBeanList)
					{
						itemList.add(new BaseItemBean(locationBean));
					}
				}
				else
				{
					loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_NORMAL);
				}
				commonNormalAdapter.setNewData(itemList);
			}

			@Override public void onFailure(String message)
			{
				loadTipFailedLayout.setState(LoadTipFailedLayout.STATE_FAILED);
				swipeToLoadLayout.setLoadingMore(false);
			}
		});
	}
}
